<template>
  <div>
    <a-modal
      centered
      :title="title"
      :width="width"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭">

      <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="文件上传" prop="fileList">
                  <j-upload :multiple="multiple" v-model="fileList" ref="upload" @ok="uploadSuccess" ></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
    </a-modal>
  </div>

</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  export default {
    name: "reNameModal",
    data(){
      return{
        model:{
        },
        width:'50%',
        fileList:[],
        fileSizeArr:[],
        currentId:'',
        currentParentId: 0,
        multiple: true,
        title:'',
        parentFileList:[],
        visible: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "ghCustomerFileInfo/add",
        }
      }
    },
    /*watch:{
      fileList:{
        immediate: true,
        handler(newValue, oldValue){
          for (let i=0;i<this.fileList.length;i++){
            that.fileUrls+=FileList[i];
            that.fileUrls+="#";
          }
          that.model.fileUrl = that.fileUrls;
          console.log(`\n两极反转: ${oldValue} → ${newValue}`);//这个方法会覆盖所以内容
        }
      }
    },*/
    computed: {
      formDisabled(){
        return this.disabled
      },

    },
    methods: {
      uploadSuccess(value){
        this.fileSizeArr = value;
      },
      add (customerId,parentId,parentName) {
        if (parentId == 12){
          this.multiple = false;
        }else {
          this.multiple = true;
        }
        this.currentParentId = parentId;
        this.model.customerId=customerId;
        this.model.parentId = parentId;
        this.model.parentName = parentName;
        this.visible = true;
      },
      close(){
        this.$emit('close');
        this.visible = false;
        this.fileList = [];
      },
      handleCancel() {
        this.close();
      },
      handleOk () {
        const that = this;
        if (this.fileList == null || this.fileList.length === 0){
          this.$message.error("请选择文件")
          return;
        }else {
          this.model.fileUrl = this.fileList;
        }
        this.model.fileSizeList = this.fileSizeArr;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }
            httpAction(httpurl,that.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok',that.model.parentId);
                that.$refs.upload.fileSizeArr = [];
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.close()
            })
          }

        })
      },
    }
  }
</script>

<style scoped>

</style>